# 作业一：用装饰器实现被除数不能为0
import time


def decorator(func):
    def wrapper(a, b):
        if b == 0:
            return "参数b不能为0"
        else:
            res = func(a, b)
        return res

    return wrapper


@decorator
def work(a, b):
    res = a / b
    print('a除b的结果为：', res)

# 作业二 实现重运行装饰器，出现AssertionError则重运行，最多运行3次

def rerun(func):
    def wrapper1(*args,**kwargs):
        for i in range(4):
            try:
                if i==0:
                    print("开始执行函数！！")
                else:
                    print(f"开始第{i}次重新执行函数")
                res=func(*args,**kwargs)
            except AssertionError as ex:
                print("执行失败")
                if i==3:
                    raise ex
            else:
                print("执行通过")
                return res

    return wrapper1

@rerun
def func2():
    expected = 'abc'
    res = input('请输入实际结果：')
    assert expected == res


# 作业3：验证用户是否登录
status={"status":False}
def login(func):
    def wrapper3(*args,**kwargs):
        if status["status"]:
            func(*args,**kwargs)
        else:
            print("您暂未登录，请登录后重试")
            name = input("请输入账号：")
            password = input("请输入密码：")
            if name == "lemonban" and password == "123456":
                status["status"]=True
                func(*args, **kwargs)
            else:
                print("您输入的账号密码有误，请重新输入")
    return wrapper3

@login
def func3_1():
    print("这是1")


@login
def func3_2():
    print("这是2")


@login
def func3_3():
    print("这是3")






# 作业4  用装饰器接收参数，当函数运行时间大于参数时打印函数名和运行时间
def decorator1(num):
    def decorator2(func4):
        def wrapper():
            start_time=time.time()
            res=func4()
            end_time=time.time()
            if end_time-start_time>num:
                print(func4)
                print(end_time-start_time)
            return res
        return wrapper
    return decorator2

@decorator1(2)
def func4():
    time.sleep(5)
    print("函数执行完毕！")





if __name__ == "__main__":
    # --------作业一--------
    print(work(3, 0))

    # ------作业二---------
    print("dd")
    func2()

    # ------作业三------
    func3_1()
    func3_2()
    func3_3()



    # -------作业四-----------
    print(func4())


